package com.absinthe.libchecker.features.chart.impl

import android.content.Context
import android.graphics.Color
import com.absinthe.libchecker.R
import com.absinthe.libchecker.database.entity.Features
import com.absinthe.libchecker.database.entity.LCItem
import com.absinthe.libchecker.features.chart.BaseChartDataSource
import com.absinthe.libchecker.utils.extensions.getColorByAttr
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.charting.formatter.PercentFormatter
import com.github.mikephil.charting.utils.MPPointF
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class KotlinChartDataSource : BaseChartDataSource<PieChart>() {
  override val classifiedList: List<MutableList<LCItem>> = listOf(mutableListOf(), mutableListOf())

  override suspend fun fillChartView(chartView: PieChart) {
    withContext(Dispatchers.Default) {
      val context = chartView.context ?: return@withContext
      val parties = listOf(
        context.resources.getString(R.string.string_kotlin_used),
        context.resources.getString(R.string.string_kotlin_unused)
      )
      val entries: ArrayList<PieEntry> = ArrayList()
      val colorOnSurface = context.getColorByAttr(com.google.android.material.R.attr.colorOnSurface)

      filteredList?.let {
        for (item in it) {
          if ((item.features and Features.KOTLIN_USED) > 0) {
            classifiedList[0].add(item)
          } else {
            classifiedList[1].add(item)
          }
        }

        // NOTE: The order of the entries when being added to the entries array determines their position around the center of
        // the chart.
        val legendList = mutableListOf<String>()
        for (i in parties.indices) {
          entries.add(PieEntry(classifiedList[i].size.toFloat(), parties[i % parties.size]))
          legendList.add(parties[i % parties.size])
        }
        val dataSet = PieDataSet(entries, "").apply {
          setDrawIcons(false)
          sliceSpace = 3f
          iconsOffset = MPPointF(0f, 40f)
          selectionShift = 5f
          xValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
          yValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
          valueLineColor = context.getColorByAttr(com.google.android.material.R.attr.colorOnSurface)
        }

        // add a lot of colors
        val colors = arrayListOf(
          Color.parseColor("#7E52FF"),
          Color.parseColor("#D9318E")
        )

        dataSet.colors = colors
        // dataSet.setSelectionShift(0f);
        val data = PieData(dataSet).apply {
          setValueFormatter(PercentFormatter())
          setValueTextSize(10f)
          setValueTextColor(colorOnSurface)
        }

        withContext(Dispatchers.Main) {
          chartView.apply {
            this.data = data
            setEntryLabelColor(colorOnSurface)
            highlightValues(null)
            invalidate()
          }
        }
      }
    }
  }

  override fun getLabelByXValue(context: Context, x: Int): String {
    return when (x) {
      0 -> context.getString(R.string.string_kotlin_used)
      1 -> context.getString(R.string.string_kotlin_unused)
      else -> ""
    }
  }
}
